package com.hnepri.util;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class DBUtil {

    public static String DRIVERNAME = null;
    public static String URL = null;
    public static String USER = null;
    public static String PASSWORD = null;

    public static Connection conn = null;

    static {
        try {

            Properties props = new Properties();
            //Reader in = new FileReader("db.properties");
            InputStream in = DBUtil.class.getClassLoader().getResourceAsStream("db.properties");
            props.load(in);

            DRIVERNAME = props.getProperty("drivername");
            URL = props.getProperty("url");
            USER = props.getProperty("user");
            PASSWORD = props.getProperty("password");

            Class.forName(DRIVERNAME);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static Connection getConnection() throws Exception {
        conn = DriverManager.getConnection(URL, USER, PASSWORD);
        return conn;
    }
    public static void closeResource(ResultSet rs) throws SQLException {
        if (rs != null) rs.close();
    }
    public static void closeResource(PreparedStatement ps, ResultSet rs) throws SQLException {
        if (rs != null) rs.close();
        if (ps != null) ps.close();
    }
    public static void closeResource(Connection conn, PreparedStatement ps) throws SQLException {
        if (ps != null) ps.close();
        if(conn != null) conn.close();
    }

    public static void closeResource(Connection conn, PreparedStatement ps, ResultSet rs) throws SQLException {
        if (rs != null) rs.close();
        if (ps != null) ps.close();
        if(conn != null) conn.close();
    }
}
